// app/models/test/index.install

const Sequelize = require('sequelize')
const config = require('../config/chat.config')

const sequelize = new Sequelize(config)
// 测试连接
sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.')
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err)
  })
const User = sequelize.import(__dirname+'/User')

//关联

//用户和文章,点赞，收藏，评论
// User.hasMany(Article, {foreignKey: 'uid'})
// Article.belongsTo(User, {foreignKey: 'uid'})
// User.hasMany(ArticleLike, {foreignKey: 'uid'})
// ArticleLike.belongsTo(User, {foreignKey: 'uid'})
// User.hasMany(ArticleStar, {foreignKey: 'uid'})
// ArticleStar.belongsTo(User, {foreignKey: 'uid'})
// User.hasMany(ArticleComment, {foreignKey: 'uid'})
// ArticleComment.belongsTo(User, {foreignKey: 'uid'})

//文章和点赞，收藏，评论
// User.hasMany(ArticleLike, {foreignKey: 'aid'})
// ArticleLike.belongsTo(User, {foreignKey: 'aid'})
// User.hasMany(ArticleStar, {foreignKey: 'aid'})
// ArticleStar.belongsTo(User, {foreignKey: 'aid'})
// User.hasMany(ArticleComment, {foreignKey: 'aid'})
// ArticleComment.belongsTo(User, {foreignKey: 'aid'})

// 同步到 Mysql 中
// 也就是将我们用 install 对象声明的模型通过 sequelize 转换成 mysql 中真正的一张数据表
//sequelize.sync({alter: true})
// 实例化，并指定配置

module.exports = {
  sequelize,
  User
};



